package copyengine.sceneAndDialog.dialog
{
    import copyengine.utils.CEStarlingUtils;
    import copyengine.utils.GeneralUtils;

    import starling.display.DisplayObjectContainer;
    import starling.display.Sprite;

    public class CEDialogManger
    {
        private static var mInstance:CEDialogManger;

        public static function get instance():CEDialogManger
        {
            if (mInstance == null) {
                mInstance = new CEDialogManger();
            }
            return mInstance;
        }

        private var mAllInViewDialogVector:Vector.<ICEDialog>;
        private var mDialogMangerContainer:Sprite;

        public function CEDialogManger()
        {
            mDialogMangerContainer = new Sprite();
            mAllInViewDialogVector = new Vector.<ICEDialog>();
        }

        public function showDialog(_dialog:ICEDialog):void
        {
            mAllInViewDialogVector.push(_dialog);
            if (_dialog.isModalDialog && _dialog.background != null) {
                mDialogMangerContainer.addChild(_dialog.background);
            }
            mDialogMangerContainer.addChild(_dialog.dialogContainer);
            _dialog.onAddToDialogManger();
        }

        public function disposeDialog(_dialog:ICEDialog):void
        {
            CEStarlingUtils.removeTargetFromParent(_dialog.background);
            CEStarlingUtils.removeTargetFromParent(_dialog.dialogContainer);
            _dialog.dispose();
            GeneralUtils.removeElementFromArrayOrVector(_dialog, mAllInViewDialogVector);
        }

        public function get dialogMangerContainer():DisplayObjectContainer {return mDialogMangerContainer;}


    }
}
